package com.chaos.meta;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.oas.annotations.EnableOpenApi;

/**
 * @author naicha
 */
@SpringBootApplication(exclude= {SecurityAutoConfiguration.class})
@MapperScan("com.chaos.meta.mapper")
@EnableOpenApi
public class MetaApplication {

    final static int defaultTimeout = 60000;

    public static void main(String[] args) {
		SpringApplication.run(MetaApplication.class, args);
	}

    /**
     * RestTemplate Bean, 解决RestTemplate由于依赖ClientHttpRequestFactory对象无法直接注入的问题。
     */
    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(defaultTimeout);
        requestFactory.setReadTimeout(defaultTimeout);
        restTemplate.setRequestFactory(requestFactory);
        return restTemplate;
    }

    /**
     *  解决由于tomcat版本不兼容导致请求url中特殊字符抛异常的问题：
     *  Exception Detail:	Invalid character found in the request target.
     *                   	The valid characters are defined in RFC 7230 and RFC 3986.
     */
    @Bean
    public ConfigurableServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addConnectorCustomizers(connector -> connector.setProperty("relaxedQueryChars", "|{}[]\\"));
        return factory;
    }
}
